/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "stdafx.h"
#include "InventoryModel.h"

#include "AbstractInventory.h"

InventoryModel::InventoryModel(AbstractInventory &inventory)
	: inventory(&inventory)
{
	// Connect the modified() to the refresh() signal so that
	// the model is kept up to date every time items are added to
	// or removed from the inventory.
	connect(&inventory, SIGNAL(modified()), this, SLOT(refresh()));

	// This ensures that the comparison function is set to _something_.
	// The program will crash otherwise...
	sort(0, Qt::AscendingOrder);
}

QModelIndex InventoryModel::index(int row, int column, const QModelIndex &parent) const
{
	return parent.isValid() ? QModelIndex() : createIndex(row, column);
}

QModelIndex InventoryModel::parent(const QModelIndex &child) const
{
	return QModelIndex();
}

int InventoryModel::rowCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : items.count();
}

int InventoryModel::columnCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : 1;
}

QVariant InventoryModel::data(const QModelIndex &index, int role) const
{
	return role == Qt::DisplayRole ? getItem(index).toString() : QVariant();
}

QVariant InventoryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
	return role == Qt::DisplayRole ? "Item" : QVariant();
}

void InventoryModel::sort(int column, Qt::SortOrder order)
{
	sortComparisonFunc = [=](const Item &left, const Item &right) -> bool
	{
		// Compare by name, then number.
		bool output = left.getName() == right.getName() ?
			left.getNumber() < right.getNumber() :
			left.getName() < right.getName();
		
		return order == Qt::AscendingOrder ? output : !output;
	};

	refresh();
}

Item InventoryModel::getItem(const QModelIndex index) const
{
	return items[index.row()];
}

void InventoryModel::refresh()
{		
	items = inventory->getItems();
	qSort(items.begin(), items.end(), sortComparisonFunc);

	reset();
}
